<template>
 <div>
   <div>
     给你一个字符串数组 words ，找出并返回 length(words[i]) * length(words[j]) 的最大值，并且这两个单词不含有公共字母。如果不存在这样的两个单词，返回 0 。
   </div>
   <div>
     示例 1：
     输入：words = ["abcw","baz","foo","bar","xtfn","abcdef"]
     输出：16
     解释：这两个单词为 "abcw", "xtfn"。
   </div>
   <div>
     学习点： 位运算左移/右移  1 << 2 => 0b100 1<< 1 => 0b10
   </div>
 </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
// 输出二进制展示 无调用
function binary (num: number, Bits = 2) {
  const resArry = []
  const xresArry = []
  let i = 0
  // 除2取余
  for (; num > 0;) {
    resArry.push(num % 2)
    num = parseInt((num / 2).toString())
    i++
  }
  // 倒序排列
  for (let j = i - 1; j >= 0; j--) {
    xresArry.push(resArry[j])
  }
  // 报错
  if (Bits < xresArry.length) {
    console.log('控制位小于转换位数')
  }
  // 补0操作
  if (Bits) {
    for (let r = xresArry.length; r < Bits; r++) {
      xresArry.unshift('0')
    }
  }
  return xresArry.join().replace(/,/g, '')
}
export default defineComponent({
  name: 'topic-1',
  title: '最大单词长度乘积',
  setup (props, context) {
    const maxProduct = function (words: string[]) {
      const length = words.length
      const masks = new Array(length).fill(0)
      for (let i = 0; i < length; i++) {
        const word = words[i]
        const wordLength = word.length
        for (let j = 0; j < wordLength; j++) {
          masks[i] |= 1 << (word.charCodeAt(j) - 'a'.charCodeAt(0))
        }
      }
      let maxProd = 0
      for (let i = 0; i < length; i++) {
        for (let j = i + 1; j < length; j++) {
          if ((masks[i] & masks[j]) === 0) {
            maxProd = Math.max(maxProd, words[i].length * words[j].length)
          }
        }
      }
      return maxProd
    }
    return {}
  }
})
</script>

<style scoped>

</style>
